package com.cn.barcode;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceManager;
import android.util.Log;

public class Setting extends PreferenceActivity implements
		OnPreferenceChangeListener, OnPreferenceClickListener {
	private ListPreference textSizeListPref;
	private ListPreference textKindListPref;
	private String textKind_value;
	private String textSize_value;
	private String textSize;
	private String textkind;
	private Editor sharedata;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settingpre);
		textKind_value = getResources().getString(R.string.textkind);
		textSize_value = getResources().getString(R.string.textsize);
		textKindListPref = (ListPreference) findPreference(textKind_value);
		textSizeListPref = (ListPreference) findPreference(textSize_value);
		textSizeListPref.setOnPreferenceChangeListener(this);
		textSizeListPref.setOnPreferenceClickListener(this);
		textKindListPref.setOnPreferenceChangeListener(this);
		textKindListPref.setOnPreferenceClickListener(this);
		SharedPreferences prefs = PreferenceManager
	                .getDefaultSharedPreferences(this);
		SharedPreferences settings = getSharedPreferences("data", 0);
		textSize = prefs.getString(textSize_value, "10");
		textkind = settings.getString("textkind", "MONOSPACE");
		Log.v("textkindtextkindtextkindtextkindtextkindtextkind", textSize);
		// check = settings.getBoolean("jugeSetting", true);
		Editor needShowset = getSharedPreferences("setting", 0).edit();
		needShowset.putBoolean("jugeneedsetting", false);
		needShowset.commit();	
		sharedata = getSharedPreferences("data", 0).edit();
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		if (preference.getKey().equals(textKind_value)) {
			Log.v("SystemSetting", "textKindListPref is Click");
		} else if (preference.getKey().equals(textSize_value)) {
			Log.v("SystemSetting", "textSizeListPref is Click");
		} else {
			// 如果返回false表示不允许被改变
			return false;
		}
		return true;
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		if (preference.getKey().equals(textKind_value)) {
			Log.v("SystemSetting", newValue + "");
			sharedata.putString("textkind", newValue.toString());
		} else if (preference.getKey().equals(textSize_value)) {
			Log.v("SystemSetting", "textSizeListPref is changed");
			sharedata.putString("textSize", newValue.toString());
		} else {
			// 如果返回false表示不允许被改变
			return false;
		}
		return true;
	}
}
